<?php
/**
 * Open/close a door
 * @package action
 * @static
 * @author Alcaitiff
 */
class Open implements Action{
    /**
     * @static
     * @param Command $command Command
     * @param int $tick Tick
     * @return string
     */
    public static function run($command, $tick) {
        //Get the char room
        $parameters=$command->parameters;
        $line=$command->command;
        $char=$command->character;
		$room=$char->getRoom();
        if(is_array($parameters) && isset($parameters[0]) && $parameters[0]!=null){
            $direction = $parameters[0];
        }else{
            MessageSystem::addErrorMessage('You must specify a direction');
            return null;
        }

		$passage=$room->getPassageByDirection($direction);
		if($passage!=null){
            if($line=='open'){
                if($passage->door->isOpen()){
                    MessageSystem::addErrorMessage('This direction is already open');
                    return null;
                }else{
                     $passage->open($char);
                     return $passage;
                }
            }else{
                if($passage->door->isClean()){
                    MessageSystem::addErrorMessage('There is no door available to close');
                    return null;
                }elseif($passage->door->isOpen()){
                    $passage->close($char);
                    return $passage;
                }else{
                    MessageSystem::addErrorMessage('This direction is already closed');
                    return null;
                }
            }
		}else{
			MessageSystem::addErrorMessage('No exit available to '.$direction);
            return null;
		}
    }
}